package com.qf.j2112.alipay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.qf.j2112.config.AliPayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author jeffrey
 * @Date 2022/3/15 14:47
 * @Description: 调用支付宝支付的组件
 */
@Component
public class AlipayTool {

    @Autowired
    private AliPayConfig alipayConfig;

    /**
     * 支付接口
     *
     * @param order
     * @return
     * @throws AlipayApiException
     */
    public String pay(AlipayOrder order) throws AlipayApiException {

        // 支付宝网关
        String serverUrl = alipayConfig.getGatewayUrl();

        // APPID
        String appId = alipayConfig.getAppId();

        // 商户私钥, 即PKCS8格式RSA2私钥
        String privateKey = alipayConfig.getPrivateKey();

        // 格式化为 json 格式
        String format = "json";

        // 字符编码格式
        String charset = alipayConfig.getCharset();

        // 支付宝公钥, 即对应APPID下的支付宝公钥
        String alipayPublicKey = alipayConfig.getPublicKey();

        // 签名方式
        String signType = alipayConfig.getSignType();

        // 页面跳转同步通知页面路径
        String returnUrl = alipayConfig.getReturnUrl();

        // 服务器异步通知页面路径
        String notifyUrl = alipayConfig.getNotifyUrl();

        // 1、获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);

        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();

        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(returnUrl);

        // 服务器异步通知页面路径  （业务处理接口地址）
        alipayRequest.setNotifyUrl(notifyUrl);

        // 封装参数(以json格式封装)  订单信息
        alipayRequest.setBizContent(JSON.toJSONString(order));


        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("result:"+result);

        // 返回付款信息
        return result;
    }

    /**
     * 验签
     * @param request
     * @return
     */
    public  boolean checkSign(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        HashMap<String ,String> paramsMap = new HashMap<>();
        requestMap.forEach((key,values)->{
            String strs ="";
            for(String str:values){
                strs =strs + str;
            }
            System.out.println(key+"==="+strs);
            paramsMap.put(key,strs);
        });
//        调用SDK进行验签
        try {
            boolean b = AlipaySignature.rsaCheckV1(paramsMap, alipayConfig.getPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType());
            System.out.println("checkSign:"+b);
            return b;
        } catch (AlipayApiException exception) {
            exception.printStackTrace();
            System.out.println("=================验签失败==============");
            return false;
        }

    }
}
